import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";
import createLogger from 'vuex/dist/logger'

import bmiModule from "./bmi.module";
import testModule from "./test.module";

Vue.use(Vuex);

// root: list, cont
// -- bannerModule:  list, cont
// -- articleModule: list, cont
// -- todoModule: list, cont
// -- ...

export default new Vuex.Store({
  plugins: [createLogger()],
  strict: true,
  state: {
    count: 0,
    banners: [],
    list: [],
  },
  getters: {
    countX10(state) {
      return state.count * 10;
    },
  },
  mutations: {
    set_count(state) {
      state.count++;
    },
    SET_BANNER(state, data) {
      state.banners = data;
    },
    SET_LIST(state, data) {
      state.list = data;
    },
  },
  actions: {
    async getBanners(context) {
      const res = await axios({
        url: "https://gank.io/api/v2/banners",
      });
      // console.log(res)
      context.commit("SET_BANNER", res.data.data);
      // console.log('commit over')
      // console.log(context)
      /*
      return axios({
        url: "https://gank.io/api/v2/banners",
      }).then((res) => {
        context.commit("SET_BANNER", res.data.data);
        // console.log(res.data.data)
      });*/
    },
    async getList({ commit }) {
      const { data } = await axios({
        url: "https://gank.io/api/v2/data/category/All/type/All/page/1/count/12",
      });
      commit("SET_LIST", data.data);
    },
  },
  modules: {
    bmi: bmiModule,
    test: testModule,
  },
});
